package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jf3q.common.domain.EduComment;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduOrder;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.mapper.EduOrderMapper;
import com.jf3q.common.service.impl.EduCommentServiceImpl;
import com.jf3q.common.service.impl.EduCourseServiceImpl;
import com.jf3q.common.service.impl.EduOrderServiceImpl;
import com.jf3q.common.vo.OrderVo;
import com.jf3q.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/order")
@Tag(name = "订单")
public class OrderController {

    @Autowired
    EduOrderServiceImpl orderService;

    @Autowired
    EduCourseServiceImpl courseService;

    @Autowired
    EduOrderMapper orderMapper;

    @Autowired
    EduCommentServiceImpl commentService;

//    生成订单的方法
    @PostMapping("/createOrder/{courseId}")
    @Operation(summary = "生成订单")
    public ResultVo createOrder(@PathVariable String courseId){
        EduCourse course=courseService.selectById(courseId);

        EduOrder order=new EduOrder();
        order.setOrderNo(UUID.randomUUID().toString().substring(0,10));//随机生成订单号
        order.setCourseId(course.getId());
        order.setCourseTitle(course.getTitle());
        order.setCourseCover(course.getCover());
        order.setMemberId(UUID.randomUUID().toString().substring(0,10));
        order.setStatus(0);
        order.setPayType(2);
        order.setTotalFee(course.getPrice());
        order.setTeacherName(course.getName());
        order.setIsDeleted(0);
        order.setEduCreate(new Date());
        order.setEduModified(new Date());

        orderService.insert(order);
        return ResultVo.success("",order.getId());
    }


    //根据订单id查询订单信息
    @GetMapping("/getOrdersInfo/{id}")
    @Operation(summary = "根据订单id查询订单信息")
    public ResultVo getOrderInfo(@PathVariable String id) {
        EduOrder eduOrder = orderMapper.selectById(id);
        return ResultVo.success("", eduOrder);
    }


    //查询订单表
    @PostMapping("/getOrderList/{courseId}")
    public ResultVo getList(@PathVariable String courseId) {
        EduOrder order = orderService.selectOrder(courseId);
        return ResultVo.success("order", order);
    }

    //根据课程id和用户id查询订单表中订单状态
    @GetMapping("isBuyCourse/{courserId}/{memberId}")
    public boolean isBuyCourese(@PathVariable String courserId, @PathVariable String memberId) {
        QueryWrapper<EduOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("courserId", courserId);
        wrapper.eq("memberId", memberId);
        wrapper.eq("status", 1);//支付状态 1代表已支付
        long count = orderService.count(wrapper);
        if (count > 0) {//已支付
            return true;
        } else {
            return false;
        }
    }


    //查询当前用户的所有订单信息
    @PostMapping("/showByMid/{memberId}")
    public ResultVo showByMid(@PathVariable String memberId) {
        EduOrder order = orderService.selectById(memberId);
        return ResultVo.success("order", order);
    }


    //查询当前用户的所有评论信息，可以修改和删除评论
    @PostMapping("/showComm/{memberId}")
    public ResultVo showComm(@PathVariable String memberId) {
        EduComment comment = commentService.selectMid(memberId);
        return ResultVo.success("comment", comment);
    }

    //修改评论
    @PostMapping("/updateComm")
    public ResultVo updateComm(@RequestBody EduComment comment) {
        try {
            commentService.saveOrUpdate(comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.success("修改成功", null);
    }


    //删除评论
    @PostMapping("/delComm/{id}")
    public ResultVo delComm(@PathVariable String id) {
        boolean b = false;
        try {
            b = commentService.removeById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("删除成功", b);
    }

}
